﻿using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Unity;
using appCtx = EasyInvoice.ApplicationContext.AppContext;
using EasyInvoice.WPF.Common;
using System.ComponentModel;

namespace EasyInvoice.WPF.ViewModel
{
    public class UserViewModel : NotifyPropertyChanged, IDataErrorInfo
    {
        private Model.User _user {get;set;}

        private Services.UsersService _serviceUsers; 

        public UserViewModel():this(new Model.User()){}

        public UserViewModel(Model.User user)
        {
            _serviceUsers = appCtx.Instance.IoC.Resolve<Services.UsersService>();
            // create new user for adding
            if (user == null) user = new Model.User(); 
            this._user = user;

            this._viewInvoices = ((UserRights)_user.Rights & UserRights.ViewInvoices) == UserRights.ViewInvoices;
            this._addInvoices = ((UserRights)_user.Rights & UserRights.AddInvoices) == UserRights.AddInvoices;
            this._editInvoices = ((UserRights)_user.Rights & UserRights.EditInvoices) == UserRights.EditInvoices;
            this._deleteInvoices = ((UserRights)_user.Rights & UserRights.DeleteInvoices) == UserRights.DeleteInvoices;
            this._printInvoices = ((UserRights)_user.Rights & UserRights.PrintInvoices) == UserRights.PrintInvoices;

            this._viewClients = ((UserRights)_user.Rights & UserRights.ViewClients) == UserRights.ViewClients;
            this._addClients = ((UserRights)_user.Rights & UserRights.AddClients) == UserRights.AddClients;
            this._editClients = ((UserRights)_user.Rights & UserRights.EditClients) == UserRights.EditClients;
            this._deleteClients = ((UserRights)_user.Rights & UserRights.DeleteClients) == UserRights.DeleteClients;

            this._viewUsers = ((UserRights)_user.Rights & UserRights.ViewUsers) == UserRights.ViewUsers;
            this._addUsers = ((UserRights)_user.Rights & UserRights.AddUsers) == UserRights.AddUsers;
            this._editUsers = ((UserRights)_user.Rights & UserRights.EditUsers) == UserRights.EditUsers;
            this._deleteUsers = ((UserRights)_user.Rights & UserRights.DeleteUsers) == UserRights.DeleteUsers;

            this._viewSettings = ((UserRights)_user.Rights & UserRights.ViewSettings) == UserRights.ViewSettings;
            this._editSettingsAppearance = ((UserRights)_user.Rights & UserRights.EditApearance) == UserRights.EditApearance;
            this._editSettingsExport = ((UserRights)_user.Rights & UserRights.EditExport) == UserRights.EditExport;
            this._editSettingsEmail = ((UserRights)_user.Rights & UserRights.EditEmail) == UserRights.EditEmail;
            this._editSettingsOptions = ((UserRights)_user.Rights & UserRights.EditOptions) == UserRights.EditOptions;
            this._editSettingsDatabase = ((UserRights)_user.Rights & UserRights.EditDatabase) == UserRights.EditDatabase;
            this._preventEditOrDelete = ((UserRights)_user.Rights & UserRights.PreventEditDelete) == UserRights.PreventEditDelete;
        }

        public int Id
        {
            get
            {
                return this._user.Id;
            }
            set
            {
                this._user.Id = value;
                this.OnPropertyChanged("Id");
            }
        }

        public string UserName
        {
            get
            {
                return this._user.UserName;
            }
            set
            {
                this._user.UserName = value;
                this.OnPropertyChanged("UserName");
            }
        }

        public string DisplayName
        {
            get
            {
                return this._user.DisplayName;
            }
            set
            {
                this._user.DisplayName = value;
                this.OnPropertyChanged("DisplayName");
            }
        }

        public UserRights Rights
        {
            get { return (UserRights)_user.Rights; }
            set {_user.Rights |=(long)value; }
        }

        private string _localPassword = string.Empty;
        private string _confirmlocalPassword = string.Empty;

        public string Password
        {
            get
            {
                //return this._user.Password;
                return string.Empty;
            }
            set
            {
                this._localPassword = value;
                this.OnPropertyChanged("Password");
            }
        }

        public string ConfirmPassword
        {
            get
            {
                //return this._user.Password;
                return string.Empty;
            }
            set
            {
                this._confirmlocalPassword = value;
                this.OnPropertyChanged("ConfirmPassword");
            }
        }

        internal void Delete()
        {
            try
            {
                this._serviceUsers.Delete(this._user);
            }
            catch (Exception ex)
            {
                ModernDialog.ShowMessage(ex.Message, "Delete User Failed", MessageBoxButton.OK);
            }
        }

        private ICommand showSaveCommand;

        public ICommand ShowSaveCommand
        {
            get
            {
                if (showSaveCommand == null)
                {
                    showSaveCommand = new Common.CommandBase(
                        i => this.SaveUser(),
                        i => ((UserViewModel)Properties.Settings.Default.AuthenticatedUser) == null ? false :
                            this.Id == 0 ? ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).AddUsers :
                            ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).EditUsers);
                }
                return showSaveCommand;
            }
        }

        private void SaveUser()
        {

            //save user
            try
            {
                UserRights rights = UserRights.None;

                if (this._viewInvoices) rights |= UserRights.ViewInvoices;
                if (this._addInvoices) rights |= UserRights.AddInvoices;
                if (this._editInvoices) rights |= UserRights.EditInvoices;
                if (this._deleteInvoices) rights |= UserRights.DeleteInvoices;
                if (this._printInvoices) rights |= UserRights.PrintInvoices;

                if (this._viewClients) rights |= UserRights.ViewClients;
                if (this._addClients) rights |= UserRights.AddClients;
                if (this._editClients) rights |= UserRights.EditClients;
                if (this._deleteClients) rights |= UserRights.DeleteClients;

                if (this._viewUsers) rights |= UserRights.ViewUsers;
                if (this._addUsers) rights |= UserRights.AddUsers;
                if (this._editUsers) rights |= UserRights.EditUsers;
                if (this._deleteUsers) rights |= UserRights.DeleteUsers;

                if (this._viewSettings) rights |= UserRights.ViewSettings;
                if (this._editSettingsAppearance) rights |= UserRights.EditApearance;
                if (this._editSettingsExport) rights |= UserRights.EditExport;
                if (this._editSettingsEmail) rights |= UserRights.EditEmail;
                if (this._editSettingsOptions) rights |= UserRights.EditOptions;
                if (this._editSettingsDatabase) rights |= UserRights.EditDatabase;

                this._user.Rights = (long)rights;
                if (this._localPassword != string.Empty && this._confirmlocalPassword == this._localPassword)
                {
                    this._user.Password = Common.Utils.ConvertToSHA512(
                                                        string.Format("{0}|{1}", UserName, this._localPassword)
                                                                        );
                }
                    this._serviceUsers.Save(this._user);
                
                //cumva sa declansez in list view model refresul
                //......
            }
            catch (Exception ex)
            {
                ModernDialog.ShowMessage(ex.Message, "Save User Failed", MessageBoxButton.OK);
            }

           
        }

        #region invoice rights
        private bool _viewInvoices;

        public bool ViewInvoices
        {
            get
            {
                return _viewInvoices;
            }
            set
            {
                this._viewInvoices = value;
                this.OnPropertyChanged("ViewInvoices");
            }
        }

        private bool _addInvoices;

        public bool AddInvoices
        {
            get
            {
                return _addInvoices;
            }
            set
            {
                this._addInvoices = value;
                this.OnPropertyChanged("AddInvoices");
                
            }
        }

        private bool _editInvoices;

        public bool EditInvoices
        {
            get
            {
                return _editInvoices;
            }
            set
            {
                this._editInvoices = value;
                this.OnPropertyChanged("EditInvoices");
            }
        }

        private bool _deleteInvoices;

        public bool DeleteInvoices
        {
            get
            {
                return _deleteInvoices;
            }
            set
            {
                this._deleteInvoices = value;
                this.OnPropertyChanged("DeleteInvoices");
            }
        }

        private bool _printInvoices;

        public bool PrintInvoices
        {
            get
            {
                return _printInvoices;
            }
            set
            {
                this._printInvoices = value;
                this.OnPropertyChanged("PrintInvoices");
            }
        }
        #endregion

        #region clients Rights
        private bool _viewClients;

        
        public bool ViewClients
        {
            get
            {
                return _viewClients;
            }
            set
            {
                this._viewClients = value;
                this.OnPropertyChanged("ViewClients");
            }
        }

        private bool _addClients;

        public bool AddClients
        {
            get
            {
                return _addClients;
            }
            set
            {
                this._addClients = value;
                this.OnPropertyChanged("AddClients");
            }
        }

        private bool _editClients;

        public bool EditClients
        {
            get
            {
                return _editClients;
            }
            set
            {
                this._editClients = value;
                this.OnPropertyChanged("EditClients");
            }
        }

        private bool _deleteClients;

        public bool DeleteClients
        {
            get
            {
                return _deleteClients;
            }
            set
            {
                this._deleteClients = value;
                this.OnPropertyChanged("DeleteClients");
            }
        }

        #endregion

        #region users Rights
        private bool _viewUsers;

        public bool ViewUsers
        {
            get
            {
                return _viewUsers;
            }
            set
            {
                this._viewUsers = value;
                this.OnPropertyChanged("ViewUsers");
            }
        }

        private bool _addUsers;

        public bool AddUsers
        {
            get
            {
                return _addUsers;
            }
            set
            {
                this._addUsers = value;
                this.OnPropertyChanged("AddUsers");
            }
        }

        private bool _editUsers;

        public bool EditUsers
        {
            get
            {
                return _editUsers;
            }
            set
            {
                this._editUsers = value;
                this.OnPropertyChanged("EditUsers");
            }
        }

        private bool _deleteUsers;

        public bool DeleteUsers
        {
            get
            {
                return _deleteUsers;
            }
            set
            {
                this._deleteUsers = value;
                this.OnPropertyChanged("DeleteUsers");
            }
        }

        #endregion

        #region Settings Rights

        private bool _viewSettings;

        public bool ViewSettings
        {
            get
            {
                return _viewSettings;
            }
            set
            {
                this._viewSettings = value;
                this.OnPropertyChanged("ViewSettings");
            }
        }

        private bool _editSettingsAppearance;

        public bool EditSettingsAppearance
        {
            get
            {
                return _editSettingsAppearance;
            }
            set
            {
                this._editSettingsAppearance = value;
                this.OnPropertyChanged("EditSettingsAppearance");
            }
        }

        private bool _editSettingsExport;

        public bool EditSettingsExport
        {
            get
            {
                return _editSettingsExport;
            }
            set
            {
                this._editSettingsExport = value;
                this.OnPropertyChanged("EditSettingsExport");
            }
        }

        private bool _editSettingsEmail;

        public bool EditSettingsEmail
        {
            get
            {
                return _editSettingsEmail;
            }
            set
            {
                this._editSettingsEmail = value;
                this.OnPropertyChanged("EditSettingsEmail");
            }
        }

        private bool _editSettingsOptions;

        public bool EditSettingsOptions
        {
            get
            {
                return _editSettingsOptions;
            }
            set
            {
                this._editSettingsOptions = value;
                this.OnPropertyChanged("EditSettingsOptions");
            }
        }

        private bool _editSettingsDatabase;

        public bool EditSettingsDatabase
        {
            get
            {
                return _editSettingsDatabase;
            }
            set
            {
                this._editSettingsDatabase = value;
                this.OnPropertyChanged("EditSettingsDatabase");
            }
        }
        #endregion

        private bool _preventEditOrDelete;

        public bool PreventEditOrDelete
        {
            get
            {
                return this._preventEditOrDelete;
            }
            set
            {
                this._preventEditOrDelete = value; 
                this.OnPropertyChanged("PreventEditOrDelete");
            }
        }

        public bool PreventEdit
        {
            get { return !this._preventEditOrDelete; }
        }
        public string Error
        {
            get { return null; }
        }
        public string this[string columnName]
        {
            get
            {
                if (columnName == "UserName")
                {
                    return string.IsNullOrEmpty(this.UserName) ? "Required value" : null;
                }
                if (columnName == "Password" || columnName == "ConfirmPassword")
                {
                    return this._localPassword!=this._confirmlocalPassword? "Paswod not identical" : null;
                }

                return null;
            }
        }
    }
}
